package Dao;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import me.gacl.domain.Team;
import me.gacl.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import buttom.DaoHelper;

import com.alibaba.fastjson.JSON;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;


import me.gacl.test.Test;

@ Repository(value = "teamDao")
public class DaoImp implements TeamDao {
//	private DaoHelper help = new DaoHelper();
	@Resource
	private SqlSessionTemplate sqlSessionTemplate;  
	 
	public SqlSessionTemplate getSqlSessionTemplate() {
		return sqlSessionTemplate;
	}

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	//��ѯ���
	public String pagesplit(HashMap<String, Object> map) {
		// TODO Auto-generated method stub

		// ���sqlsession
		//SqlSession session = help.conn();

		String statement1 = "me.gacl.mapping.userMapper.filterlist";
		String statement2 = "me.gacl.mapping.userMapper.count_filter";
	  
	
		int RowCount = sqlSessionTemplate.selectOne(statement2, map);

		List<User> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
		ArrayList<Object> list0 = new ArrayList<Object>();
		for (User a : listUsers0) {
			HashMap<String, String> map0 = new HashMap<String, String>();
			map0.put("teamnumber", a.getNumber() + "");
			map0.put("teamname", a.getTeam());
			String jsonString0 = JSON.toJSONString(map0);
			list0.add(jsonString0);
		}
		String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
				+ "}";
		return list;
	   

	   }
	 
	  //�������
	  public int add(HashMap<String, Object>  map){
		
		  
	    //SqlSession session = help.conn();
		

		String statement = "me.gacl.mapping.userMapper.insertTeam";
		int flag = sqlSessionTemplate.insert(statement, map);
		//sqlSessionTemplate.commit();
		
		
		return flag;
		  
	  }
	  
	//�༭���
	  public int destroy(HashMap<String, Object>  map){
		
		  
	    //SqlSession session = help.conn();
		

		String statement = "me.gacl.mapping.userMapper.deleteTeam";
		int flag = sqlSessionTemplate.update(statement,map);
		//sqlSessionTemplate.commit();

		
		return flag;
		  
	  }
	  
	//ɾ�����
	  public int edit(HashMap<String, Object>  map){
		
		  
	    //SqlSession session = help.conn();
		

	    String statement = "me.gacl.mapping.userMapper.updateTeam";
        int flag = sqlSessionTemplate.update(statement,map);
        //sqlSessionTemplate.commit();
		
        System.out.println(flag);
		return flag;
		
		  
	  }
	  
	  
	  public HashMap<String, String> searchTeam(HashMap<String, Object> map) {
			// TODO Auto-generated method stub

			// ���sqlsession
			//SqlSession session = help.conn();

			String statement1 = "me.gacl.mapping.userMapper.filterlist";
			String statement2 = "me.gacl.mapping.userMapper.count_filter";
		  
		
			int RowCount = sqlSessionTemplate.selectOne(statement2, map);

			List<User> listUsers0 = sqlSessionTemplate.selectList(statement1, map);
			ArrayList<Object> list0 = new ArrayList<Object>();
			for (User a : listUsers0) {
				HashMap<String, String> map0 = new HashMap<String, String>();
				map0.put("teamnumber", a.getNumber() + "");
				map0.put("teamname", a.getTeam());
				String jsonString0 = JSON.toJSONString(map0);
				list0.add(jsonString0);
			}
			String list = "{\"total\" : " + RowCount + ", \"rows\" : " + list0
					+ "}";
			HashMap<String, String> together = new HashMap<String, String> ();
			together.put("searchlist",list);
			together.put("rowcount",RowCount+"");
			return together;
		   

		   }
	  
	  //��֤���
	  public int sign(HashMap<String, Object>  map){
		
		  
	    //SqlSession session = help.conn();
		

		String statement = "me.gacl.mapping.userMapper.checklogin";
		 Team  gin = sqlSessionTemplate.selectOne(statement, map);
		 System.out.println("Dao: "+gin);
		
		int flag ;
		 if(gin!=null){
	    	  flag = 1;
	    	  return flag;
	       }else{
	    	   flag = 0;
		      return flag;
	       }
		  
	  }
	  
	//��֤���ʹ��TeamBean
	  public int signex(Team team){
		
		  
	    //SqlSession session = help.conn();
		

		String statement = "me.gacl.mapping.userMapper.checklogin";
		 Team  gin = sqlSessionTemplate.selectOne(statement, team);
		 System.out.println("Dao: "+gin);
		
		int flag ;
		 if(gin!=null){
	    	  flag = 1;
	    	  return flag;
	       }else{
	    	   flag = 0;
		      return flag;
	       }
		  
	  }
	  public String luanma(HashMap<String, Object>  map){
		  String statement = "me.gacl.mapping.userMapper.luanma";
		  Team  luanma = sqlSessionTemplate.selectOne(statement, map);
		  System.out.println("Dao: "+luanma);
		  String team = luanma.getTeam();
		  return team;
		  
		  
	  }


	
}
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	   /*
	   public int Rowcount(String statement,HashMap<String, Object>  map){
		   
		   DaoHelper help = new DaoHelper();
		   SqlSession session = help.conn();
			 
		    //ʹ�������������mybatis�������ļ�����Ҳ���ع�����ӳ���ļ���
		  
		   int RowCount = session.selectOne(statement,map);
		   
		   return RowCount;

		   
	   }*/
	
	


